package com.lemon.stock.ui

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import com.google.accompanist.pager.ExperimentalPagerApi
import com.lemon.stock.compose.StockScreen
import com.lemon.stock.theme.StockTheme

class StockActivity : ComponentActivity() {

    private val stockId by lazy{
        intent?.getStringExtra(STOCKID) ?: ""
    }
    private val stockIds by lazy{
        intent?.getStringArrayExtra(STOCKIDS) ?:null
    }

    private val currentIndex by lazy {
        intent?.getIntExtra(CURRENT, -1)
    }

    @ExperimentalPagerApi
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            StockTheme{
                StockScreen(stockIDs = stockIds!!, currentIndex = currentIndex!!){
                    //onBackPressed()
                    setResult(Activity.RESULT_OK, Intent().putExtra("reload", "yes"))
                    finish()
                }
            }

        }
    }

    companion object{
        const val STOCKID = "STOCKID"
        const val STOCKIDS = "STOCKIDS"
        const val CURRENT = "CURRENT"
        fun newIntent(context: Context, stockId: String) =
            Intent(context, StockActivity::class.java).apply{
                putExtra(STOCKID, stockId)
            }
        fun newIntent(context: Context, stockIds: Array<String>, currentIndex: Int) =
            Intent(context, StockActivity::class.java).apply{
                putExtra(STOCKIDS, stockIds)
                putExtra(CURRENT, currentIndex)
            }
    }

}